import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import main from './route';
import { store } from '@/store';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import { NextLoading } from '@/utils/loading';

const routes: Array<RouteRecordRaw> = [
    ...main
];

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes,
	// scrollBehavior(to, from, saveScrollPosition) {
	// 	if(saveScrollPosition){
	// 		return saveScrollPosition;
	// 	}else{
	// 		return { left: 0, top: 0 };
	// 	}
	// },
})

router.beforeEach(async (to,from,next)=>{
	NProgress.configure({ showSpinner: false });
	NProgress.start();
	NextLoading.start();
    // try {
    //     if(!store.state.user.isLogin){
	// 		const u = await getUserinfo<any>()
	// 		if(u.code == 0 && u['data']['login']){
	// 			store.commit('login',u.data);
	// 		}
    //     }
    // } catch (error) {
    //     console.log(error)
    // }
	console.log(to.meta.isLogin)
	console.log(!store.state.userInfo.isLogin)
	if(to.meta.isLogin){
		if(!store.state.userInfo.isLogin){
			NProgress.done();
			NextLoading.done();
			next({name:"Login"});
		}else{
			next();
		}
	}else{
		next();
	}
})

// 路由加载后
router.afterEach(() => {
	NProgress.done();
	NextLoading.done();
});

export default router
